//
//  UserCallMenuView.m
//  EMMApp
//
//  Created by Mac on 16/11/1.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "UserCallMenuView.h"
#import "UserCallMenuCell.h"
#import "UserCallList.h"

@interface UserCallMenuView ()<UITableViewDelegate,UITableViewDataSource>
{
    NSString  *current;
}
@property (nonatomic, strong) NSMutableArray *listArr;

@property (nonatomic, strong) NSString *titleString;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation UserCallMenuView

- (id)initWithFrame:(CGRect)frame title:(NSString *)title nameArr:(NSMutableArray *)nameArr {
    if (self == [super initWithFrame:frame]) {
        _listArr = nameArr;
        _titleString = title;
        [self showUI];
    }
    return  self;
}

- (void)showUI {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    view.backgroundColor = [UIColor clearColor];
    view.userInteractionEnabled = YES;
    [self addSubview:view];
    
    //
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [view addGestureRecognizer:tap];
    
    //
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(20, (self.bounds.size.height-200)/2, self.bounds.size.width-40, 200)];
    subView.backgroundColor = [UIColor whiteColor];
    subView.userInteractionEnabled = YES;
    subView.layer.masksToBounds = YES;
    subView.layer.cornerRadius = 5;
    [self addSubview:subView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, subView.frame.size.width, 20)];
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = _titleString;
    [subView addSubview:titleLabel];
    
    UIView *iv1 = [[UIView alloc] initWithFrame:CGRectMake(0, 49, subView.frame.size.width, 1)];
    iv1.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    [subView addSubview:iv1];

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, subView.frame.size.width, subView.frame.size.height-100) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [subView addSubview:_tableView];
    
    UIView *iv2 = [[UIView alloc] initWithFrame:CGRectMake(0, 149, subView.frame.size.width, 1)];
    iv2.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    [subView addSubview:iv2];
    
    UIView *viele = [[UIView alloc] initWithFrame:CGRectMake(0, 150, subView.frame.size.width, 50)];
    viele.backgroundColor = [UIColor whiteColor];
    [subView addSubview:viele];
    
    UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    okBtn.frame = CGRectMake(0, 0, viele.frame.size.width/2, 50);
    okBtn.backgroundColor = [UIColor orangeColor];
    [okBtn setTitle:@"确定" forState:UIControlStateNormal];
    [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    okBtn.layer.masksToBounds = YES;
//    okBtn.layer.cornerRadius = 5;
    [okBtn addTarget:self action:@selector(okBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [viele addSubview:okBtn];
    
    UIButton *calnBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    calnBtn.frame = CGRectMake(viele.frame.size.width/2, 0, viele.frame.size.width/2, 50);
    calnBtn.backgroundColor = [UIColor whiteColor];
    [calnBtn setTitle:@"取消" forState:UIControlStateNormal];
    [calnBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    calnBtn.layer.masksToBounds = YES;
//    calnBtn.layer.cornerRadius = 5;
//    [calnBtn.layer setBorderWidth:1.0];
    calnBtn.layer.borderColor=[UIColor colorFromHexCode:@"#CCCCCC"].CGColor;
    [calnBtn addTarget:self action:@selector(calnBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [viele addSubview:calnBtn];
}

- (void)okBtnClick:(UIButton *)bnt {
    if ([NSString isStringEmpty2:current] == YES) {
        return;
    }
    UserCallList *list = [_listArr objectAtIndex:[current integerValue]];

    if (_touchUserCallMenuViewBlock) {
        _touchUserCallMenuViewBlock(list.telePhone,list.telePhoneType);
    }
}

- (void)calnBtnClick:(UIButton *)bnt {
    if (_touchUserCallMenuViewBlock) {
        _touchUserCallMenuViewBlock(@"",@"");
    }
}

- (void)tapClick:(UITapGestureRecognizer *)tap {
    if (_touchUserCallMenuViewBlock) {
        _touchUserCallMenuViewBlock(@"",@"");
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _listArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"UserCallMenuCellId";
    UserCallMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"UserCallMenuCell" owner:self options:nil] lastObject];
    }
    UILabel *l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 49, self.frame.size.width, 1)];
    l1.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    [cell addSubview:l1];
    UserCallList *list = [_listArr objectAtIndex:indexPath.row];
    if ([list.telePhoneType isEqualToString:@"1"]) {
        cell.titleLabel.text = [NSString stringWithFormat:@"手机【%@】",list.telePhone];
    }else {
        cell.titleLabel.text = [NSString stringWithFormat:@"固话【%@】",list.telePhone];
    }
    if ([NSString isStringEmpty2:current] == NO) {
        if (indexPath.row == [current integerValue]) {
            cell.seleImageV.image = [UIImage imageNamed:@"choose"];
        }else {
            cell.seleImageV.image = [UIImage imageNamed:@"choose_no"];
        }
    }else {
        cell.seleImageV.image = [UIImage imageNamed:@"choose_no"];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    current = [NSString stringWithFormat:@"%ld",indexPath.row];
    [_tableView reloadData];
}

@end
